﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMicroblogAPI;
using AMicroblogAPI.Common;
using AMicroblogAPI.DataContract;

namespace weibo
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            try
            {
                long userID = long.Parse(Login.accessToken.UserID);
                UserInfo ui = AMicroblog.GetUserInfo(userID);
                lbShowName.Text = ui.ScreenName;
                label3.Text = "关注 " + ui.FriendsCount;
                label4.Text = "粉丝 " + ui.FollowersCount;
                label5.Text = "微博 " + ui.StatusesCount;
                lbHomeInitial();
                loadFriends();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void lbHomeInitial()
        {
                      lvHome.GridLines = true;
            lvHome.View = View.Details;
            lvHome.LabelEdit = false;
            lvHome.Scrollable = true;
            lvHome.FullRowSelect = false;
            lvHome.Columns.Add("StatusID");
            lvHome.Columns.Add("ScreenName");
            lvHome.Columns.Add("Statues");
            lvHome.Items.Clear();
            List<StatusInfo> recentStatus = new List<StatusInfo>();
            foreach (UserInfo users in AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items)
            {
                recentStatus.AddRange(AMicroblog.GetUserStatuses(users.ID).Items);
            }
            recentStatus.AddRange(AMicroblog.GetUserStatuses(long.Parse(Login.accessToken.UserID)).Items);

            recentStatus.Sort(compareStatus);
            foreach (StatusInfo status in recentStatus)
            {
                ListViewItem lvi = new ListViewItem(new string[] { status.ID.ToString(), status.User.ScreenName, status.Text });
                lvHome.Items.Add(lvi);
            }
        }

        private static int compareStatus(StatusInfo a, StatusInfo b)
        {
            return a.ID > b.ID ? 1 : 0;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure to exit?", "...", MessageBoxButtons.OKCancel);
            e.Cancel = false;
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }

        private void tbNewEvent_TextChanged_1(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbNewEvent.Text.Trim()))
            {
                btnSubmitEvent.Enabled = true;
            }
            else
            {
                btnSubmitEvent.Enabled = false;
            }
            int count = 140 - tbNewEvent.Text.Trim().Length;
            lbHit.ForeColor = Color.Black;
            if (count < 0)
            {
                lbHit.Text = "话说得太多啦～～";
                lbHit.ForeColor = Color.Red;
                btnSubmitEvent.Enabled = false;
                return;
            }
            string hit = "请文明发言，还可以输入" + count + "字符";
            lbHit.Text = hit;
        }

        private void btnSubmitEvent_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateStatusInfo newStatus = new UpdateStatusInfo();
                newStatus.Status = tbNewEvent.Text.Trim();
                AMicroblog.PostStatus(newStatus);
                tbNewEvent.Text = string.Empty;
                lbHomeInitial();
                label5.Text = "微博 " + AMicroblog.GetUserInfo(long.Parse(Login.accessToken.UserID)).StatusesCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void loadFriends()
        {
            try
            {
                lvFriend.GridLines = true;
                lvFriend.View = View.Details;
                lvFriend.LabelEdit = false;
                lvFriend.Scrollable = true;
                lvFriend.FullRowSelect = false;
                lvFriend.Columns.Add("ScreenName");
                lvFriend.Columns.Add("Gender");
                lvFriend.Columns.Add("Followres");
                lvFriend.Columns.Add("Friends");
                lvFriend.Columns.Add("Statuses");
                foreach (UserInfo users in AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items)
                {
                    ListViewItem lvi = new ListViewItem(new string[] { users.ScreenName,users.Gender, users.FollowersCount.ToString(),
                    users.FriendsCount.ToString(), users.StatusesCount.ToString() });
                    lvFriend.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void btnAddFriend_Click(object sender, EventArgs e)
        {
            try
            {
                int ori = AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items.Count();
                AddFriends af = new AddFriends();
                af.ShowDialog();
                if (ori != AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items.Count())
                {
                    lvFriend.Items.Clear();
                    foreach (UserInfo users in AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items)
                    {
                        ListViewItem lvi = new ListViewItem(new string[] { users.ScreenName,users.Gender, users.FollowersCount.ToString(),
                    users.FriendsCount.ToString(), users.StatusesCount.ToString() });
                        lvFriend.Items.Add(lvi);
                    }
                    label3.Text = "关注 " + AMicroblog.GetUserInfo(long.Parse(Login.accessToken.UserID)).FriendsCount;
                    lbHomeInitial();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void lvFriend_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (lvFriend.SelectedItems.Count > 0)
                {
                    int ori = AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items.Count;
                    string selectItem = lvFriend.SelectedItems[0].Text;
                    DetailFriends df = new DetailFriends(selectItem);
                    df.ShowDialog();
                    if (ori != AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items.Count)
                    {
                        lvFriend.Items.Clear();
                        foreach (UserInfo users in AMicroblog.GetFriends(long.Parse(Login.accessToken.UserID)).Items)
                        {
                            ListViewItem lvi = new ListViewItem(new string[] { users.ScreenName, users.Gender, users.FollowersCount.ToString(), users.FriendsCount.ToString(), users.StatusesCount.ToString() });
                            lvFriend.Items.Add(lvi);
                        }
                        label3.Text = "关注 " + AMicroblog.GetUserInfo(long.Parse(Login.accessToken.UserID)).FriendsCount;
                        lbHomeInitial();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void lvHome_DoubleClick(object sender, EventArgs e)
        {
            //    MessageBox.Show(this, lvHome.SelectedItems[0].Text);
            try
            {
                if (lvHome.SelectedItems.Count > 0)
                {
                    ListViewItem lvi = lvHome.SelectedItems[0];
                    int ori = AMicroblog.GetUserInfo(long.Parse(Login.accessToken.UserID)).StatusesCount;
                    DetailStatus ds = new DetailStatus(long.Parse(lvHome.SelectedItems[0].Text));
                    ds.ShowDialog();
                    if (ori != AMicroblog.GetUserInfo(long.Parse(Login.accessToken.UserID)).StatusesCount)
                    {
                        lvHome.Items.RemoveAt(lvi.Index);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }


    }
}